﻿/// -------页面描述：系统日志数据访问类
/// -------创建日期：2010年05月20日 
/// -------创建人：wang
namespace webnew.DAL
{
    using System;
    using System.Text;
    using System.Data;
    using System.Data.SqlClient;
    using System.Collections.Generic;
    using webnew;
    using webnew.Model;
    using webnew.DBUtility;


    public class Sys_LogHelper
    {

        private SqlHelper DbHelper;

        public Sys_LogHelper()
        {
            DbHelper = new SqlHelper();
        }

        /// <summary>
        /// 新增一个实体
        /// </summary>
        public int Add(Model.Sys_LogInfo model)
        {
            System.Text.StringBuilder strSql = new System.Text.StringBuilder();
            strSql.Append("INSERT INTO Sys_Log(");
            strSql.Append("Id,UserID,IP,Operate,Status,Module,BlogContent,CreateDate)");
            strSql.Append("VALUES(");
            strSql.Append("@In_Id,@In_UserID,@In_IP,@In_Operate,@In_Status,@In_Module,@In_BlogContent,@In_CreateDate)");
            System.Data.SqlClient.SqlParameter[] CmdParams = new System.Data.SqlClient.SqlParameter[] {
                    DbHelper.CreateInSqlParameter("@In_Id",DbType.AnsiString,Guid.NewGuid().ToString()),
                    DbHelper.CreateInSqlParameter("@In_UserID",DbType.String,model.UserID),
                    DbHelper.CreateInSqlParameter("@In_IP",DbType.AnsiString,model.IP),
                    DbHelper.CreateInSqlParameter("@In_Operate",DbType.AnsiString,model.Operate),
                    DbHelper.CreateInSqlParameter("@In_Status",DbType.AnsiString,model.Status),
                    DbHelper.CreateInSqlParameter("@In_Module",DbType.AnsiString,model.Module),
                    DbHelper.CreateInSqlParameter("@In_BlogContent",DbType.AnsiString,model.BlogContent),
                    DbHelper.CreateInSqlParameter("@In_CreateDate",DbType.DateTime,model.CreateDate)};
            return DbHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), CmdParams);
        }

        ///// <summary>
        ///// 根据ID修改一个实体
        ///// </summary>
        //public int Modify(Model.Sys_LogInfo model)
        //{
        //    System.Text.StringBuilder strSql = new System.Text.StringBuilder();
        //    strSql.Append("UPDATE Sys_Log SET ");
        //    strSql.Append("CreatorID = @In_CreatorID,");
        //    strSql.Append("IP = @In_IP,");
        //    strSql.Append("Operate = @In_Operate,");
        //    strSql.Append("Status = @In_Status,");
        //    strSql.Append("Module = @In_Module,");
        //    strSql.Append("BlogContent = @In_BlogContent,");
        //    strSql.Append("CreateDate = @In_CreateDate");
        //    strSql.Append(" WHERE ID=@In_ID ");
        //    System.Data.SqlClient.SqlParameter[] CmdParams = new System.Data.SqlClient.SqlParameter[] {
        //            DbHelper.CreateInSqlParameter("@In_CreatorID",DbType.AnsiString,model.CreatorID),
        //            DbHelper.CreateInSqlParameter("@In_IP",DbType.AnsiString,model.IP),
        //            DbHelper.CreateInSqlParameter("@In_Operate",DbType.AnsiString,model.Operate),
        //            DbHelper.CreateInSqlParameter("@In_Status",DbType.AnsiString,model.Status),
        //            DbHelper.CreateInSqlParameter("@In_Module",DbType.AnsiString,model.Module),
        //            DbHelper.CreateInSqlParameter("@In_BlogContent",DbType.AnsiString,model.BlogContent),
        //            DbHelper.CreateInSqlParameter("@In_CreateDate",DbType.DateTime,model.CreateDate),
        //            DbHelper.CreateInSqlParameter("@In_Id",DbType.AnsiString,model.Id)};
        //    return DbHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), CmdParams);
        //}

        ///// <summary>
        ///// 根据ID删除一个实体
        ///// </summary>
        //public int Del(int id)
        //{
        //    System.Text.StringBuilder strSql = new System.Text.StringBuilder();
        //    strSql.Append("UPDATE Sys_Log SET isdel=1 ");
        //    strSql.Append("WHERE id=" + id.ToString());
        //    return DbHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), null);
        //}

        /// <summary>
        /// 获取列表数据
        /// </summary>
        public List<Model.Sys_LogInfo> GetList()
        {
            System.Text.StringBuilder strSql = new System.Text.StringBuilder();
            strSql.Append("select * from Sys_Log ");
            System.Data.SqlClient.SqlDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString(), null);
            return GetList(dr);
        }

        /// <summary>
        /// 根据ID获取一个实体
        /// </summary>
        public Model.Sys_LogInfo GetModel(string id)
        {
            Model.Sys_LogInfo model = new Model.Sys_LogInfo();
            System.Text.StringBuilder strSql = new System.Text.StringBuilder();
            strSql.Append("select * from Sys_Log WHERE id= '" +id.ToString()+ "'");
            System.Data.SqlClient.SqlDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString(), null);
            for (; dr.Read(); )
            {
                model = GetModel(dr);
            }
            dr.Close();
            return model;
        }

        /// <summary>
        /// 获取一个实体
        /// </summary>
        private Model.Sys_LogInfo GetModel(System.Data.SqlClient.SqlDataReader dr)
        {
            Model.Sys_LogInfo model = new Model.Sys_LogInfo();
            model.Id = dr["Id"].ToString();
            model.Sort = Common.MyConvert.toInt32(dr["Sort"].ToString());
            model.UserID = dr["UserID"].ToString();
            model.IP = dr["IP"].ToString();
            model.Operate = dr["Operate"].ToString();
            model.Status = dr["Status"].ToString();
            model.Module = dr["Module"].ToString();
            model.BlogContent = dr["BlogContent"].ToString();
            if (dr["CreateDate"].ToString() != "")
            {
                model.CreateDate = DateTime.Parse(dr["CreateDate"].ToString());
            }
            return model;
        }

        /// <summary>
        /// 获取一个列表
        /// </summary>
        public List<Model.Sys_LogInfo> GetList(System.Data.SqlClient.SqlDataReader dr)
        {
            List<Model.Sys_LogInfo> Lst = new List<Model.Sys_LogInfo>();
            for (; dr.Read(); )
            {
                Lst.Add(GetModel(dr));
            }
            dr.Close();
            return Lst;
        }

        /// <summary>
        /// 得到数据条数
        /// </summary>
        public int GetCount(string tabname, string condition)
        {
            return DbHelper.GetCount(tabname, condition);
        }


        /// <summary>
        /// 返回分页数据
        /// </summary>
        public List<Model.Sys_LogInfo> GetPageList(string field,string condition, int pageSize, int pageIndex, string tbNames, string sortNames, bool sortType)
        {

            return GetList(DbHelper.GetPageList(field,condition, pageSize, pageIndex, tbNames, sortNames, sortType));

        }
        ///// <summary>
        ///// 获取一个列表
        ///// </summary>
        //public List<Model.Sys_LogInfo> GetList(System.Data.SqlClient.SqlDataReader dr)
        //{
        //    List<Model.Sys_LogInfo> Lst = new List<Model.Sys_LogInfo>();
        //    for (; dr.Read(); )
        //    {
        //        Lst.Add(GetModel(dr));
        //    }
        //    dr.Close();
        //    return Lst;
        //}

        /// <summary>
        /// 根据用户表ID获取用户名
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public string GetTrueName(string userID)
        {
            System.Text.StringBuilder strSql = new StringBuilder();
            strSql.Append( "select trueName from sys_user where id = '" + userID + "' ");
            return Common.MyConvert.toString(DbHelper.ExecuteScalar(strSql.ToString()));
        }

        /// <summary>
        /// 根据用户表ID获取用户ID
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public string GetLoginID(string userID)
        {
            System.Text.StringBuilder strSql = new StringBuilder();
            strSql.Append("select loginID from sys_user where id ='" + userID + "' ");
            return Common.MyConvert.toString(DbHelper.ExecuteScalar(strSql.ToString()));
        
        }

        ///// <summary>
        ///// 获取当前最大的序号
        ///// </summary>
        ///// <returns></returns>
        //public int GetMaxSort()
        //{
        //    System.Text.StringBuilder strSql = new StringBuilder();
        //    strSql.Append("select max(sort) from sys_log");
        //    return Common.MyConvert.toInt32(DbHelper.ExecuteScalar(strSql.ToString()));
        //}
    }
}
